package com.test;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.Test;

import java.util.Calendar;
import java.util.Date;

public class Demo01 {
    @Test
    public void getToken() {
        System.out.println("..........asdhasda");
        //设置时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 300);

        /*Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");*/

        String token = JWT.create()
                //设置header
                //.withHeader(map);
                //设置payload
                .withClaim("username","tom")
                .withClaim("userid",12)
                .withExpiresAt(calendar.getTime())
                //设置signature
                .sign(Algorithm.HMAC256("adfs24!3as"));

        System.out.println(token);
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NjgxNTUwMTQsInVzZXJpZCI6MTIsInVzZXJuYW1lIjoidG9tIn0.HELFd9olBUbiDL2qRINta3lVvQ2u37PmE3y958XD1f0
    }

    @Test
    public void resolverToken() {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256("adfs24!3as")).build();
        DecodedJWT decodedJWT = verifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NjgyMjQ1MTksInVzZXJpZCI6MTIsInVzZXJuYW1lIjoidG9tIn0.msx_NUZoHeJm2EwCyiSP8lTDhgIFFHUf1IW736-ZEX0");

        String username = decodedJWT.getClaim("username").asString();
        Integer userid = decodedJWT.getClaim("userid").asInt();
        Date date = decodedJWT.getExpiresAt();

        System.out.println(username + "..." + userid + "..." + date);
    }
}
